package OutputStream;

import InputStream.FileInputStream_;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @author 王艺锡
 * @version 1.0
 */
public class FileCopy {
    public static void main(String[] args) {
        String srcFilePath = "e:\\image.png";
        String destFilePath = "d:\\image.png";

        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        int readLen = 0;
        try {
            fileInputStream = new FileInputStream(srcFilePath);
            fileOutputStream = new FileOutputStream(destFilePath);
            byte buf[] = new byte[1024];
            //从src文件中读取数据
            while ((readLen = fileInputStream.read(buf)) != -1) {
                //将每次读取到的数据写入到dest文件中
                fileOutputStream.write(buf, 0, readLen);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //如果fileInputStream和fileOutputStream不为空，则关闭流

            try {
                //一开始初始化为空，所以判断不为空则关闭
                if (fileInputStream != null) {
                    fileInputStream.close();
                }
                if (fileOutputStream != null) {
                    fileOutputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }
}
